Ext.define('mvcCouch.controller.Suppliers', {
    extend: 'Ext.app.Controller',
    stores: ['Suppliers', 'Contacts'],
    models: ['Supplier', 'Contact'],
    views: [
        'supplier.List',
		'supplier.SupplierEdit',
		'supplier.Contact',
		'supplier.ContactEdit'
    ],
    refs:[
          {
           ref:'listgrid',
           selector:'viewport > supplierlist'
           },
           {
           ref:'contactgrid',
           selector:'viewport > suppliercontact'
           }
    ],
    init: function() {
        this.control({
            'viewport > supplierlist': {
                itemdblclick: this.editSupplier,
                itemclick: this.showContact,
                selectionchange: this.clearDetails
            },
            'viewport > suppliercontact': {
                itemdblclick: this.editContact
            },
            'suppliercontact button[action=savedoc]': {
                click: this.updateDoc
            },
            'contactedit button[action=save]': {
                click: this.updateContact
            },
            'supplieredit button[action=save]': {
                click: this.updateSupplier
            }
        });
    },
    editSupplier: function(grid, record) {
        var view = Ext.widget('supplieredit');
        view.down('form').loadRecord(record);
    },
    editContact: function(grid, record) {
        var view = Ext.widget('contactedit');
        view.down('form').loadRecord(record);
    },
    showContact: function(grid, record, rowIndex) {
        this.rowidx = rowIndex.rowIndex-1;
		docinstance =  Ext.data.StoreManager.lookup('Suppliers').getAt(this.rowidx);
        suppliersinstance =  docinstance.contacts();
        if (suppliersinstance.data.items.length > 0) {
            this.getContactgrid().getStore().loadRecords(suppliersinstance.getRange(0,suppliersinstance.getCount()),{addRecords: false});
        }
    },
    updateSupplier: function(button) {
        var win    = button.up('window'),
            form   = win.down('form'),
            record = form.getRecord(),
            values = form.getValues();
        record.set(values);
        win.close();
		this.getSuppliersStore().sync();
        alert('保存成功');
        this.getSuppliersStore().load();
    },
	updateContact: function(button) {
		var win    = button.up('window'),
            form   = win.down('form'),
            record = form.getRecord(),
            values = form.getValues();
        record.set(values);
        win.close();
        this.getContactgrid().getStore().loadRecords(suppliersinstance.getRange(0,suppliersinstance.getCount()),{addRecords: false});
	},	
    updateDoc: function(button) {
        //sync parent and/or nested "inner" model instances in Users store - writes all JSON to server
        this.getSuppliersStore().sync();
        alert('保存成功');
        this.getSuppliersStore().load();
    },
    clearDetails: function() {
        //console.log('userList event has fired!');
        this.getContactsStore().removeAll();
    }
});